package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.commons.core.dto.PageInfo;
import com.his.commons.core.exception.HisException;
import com.his.commons.web.util.WebBeanUtils;
import com.his.controller.form.ModifyRoleForm;
import com.his.controller.form.NewRoleForm;
import com.his.controller.form.PageForm;
import com.his.controller.info.RoleInfo;
import com.his.model.Role;
import com.his.mapper.RoleMapper;
import com.his.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.util.StringToTrimUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 梅津铧
 * @since 2022-03-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    /**
     * 注册角色
     *
     * @param newRoleForm 新角色信息
     */
    @Override
    public boolean increaseRole(NewRoleForm newRoleForm) throws HisException {
        //判断信息是否含有空字符
        if(!StringToTrimUtil.StringToTrim(newRoleForm.getRoleName())){
            throw new HisException("请输入正确信息",500);
        }

        //注册角色
        Role roleByname = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_name", newRoleForm.getRoleName()));
        if(null != roleByname) throw new HisException("该角色已存在",500);
        Role role = new Role();
        role.setRoleName(newRoleForm.getRoleName());
        int insert = roleMapper.insert(role);
        if(insert<=0) throw new HisException("角色注册失败",500);
        return true;
    }

    /**
     * 更新角色信息
     *
     * @param modifyRoleForm  角色信息
     */
    @Override
    public boolean modifyRole(ModifyRoleForm modifyRoleForm) throws HisException {
        //判断信息是否含有空字符
        if(!StringToTrimUtil.StringToTrim(modifyRoleForm.getRoleName()) | !StringToTrimUtil.StringToTrim(String.valueOf(modifyRoleForm.getRoleId()))){
            throw new HisException("请输入正确信息",500);
        }

        //更新角色信息
        Role role = new Role();
        role.setRoleName(modifyRoleForm.getRoleName());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",modifyRoleForm.getRoleId());
        int update = roleMapper.update(role, wrapper);
        if(update<=0) throw new HisException("更新角色信息失败，请稍后重试",500);

        return true;
    }

    /**
     * 获取角色信息
     *
     * @param pageForm 分页信息
     */
    @Override
    public PageInfo<RoleInfo> obtainRoles(PageForm pageForm) throws HisException {
        //判断分页信息的参数是否合格
        if(!StringToTrimUtil.StringToTrim(String.valueOf(pageForm.getPagesize())) | !StringToTrimUtil.StringToTrim(String.valueOf(pageForm.getPagenum()))){
            throw new HisException("请输入正确的信息",500);
        }
        if(pageForm.getPagesize()<=0 | pageForm.getPagesize()<=0) throw new HisException("查询信息不符合要求",500);
        //分页查询角色信息
        Page<Role> rolePage = new Page<>();
        rolePage.setCurrent(pageForm.getPagenum());
        rolePage.setSize(pageForm.getPagesize());
        Page<Role> rp = roleMapper.selectPage(rolePage, null);
        if(null == rp.getRecords()) throw new HisException("查询失败",500);
        PageInfo<RoleInfo> roleInfoPageInfo = new PageInfo<>();
        roleInfoPageInfo.setPageNum(rp.getCurrent());
        roleInfoPageInfo.setPageSize(rp.getSize());
        roleInfoPageInfo.setTotalPage(rp.getTotal());
        roleInfoPageInfo.setRecord(WebBeanUtils.copyList(rp.getRecords(),RoleInfo.class));

        return roleInfoPageInfo;
    }

    /**
     * 注销角色
     *
     * @param id  角色id
     */
    @Override
    public boolean logoutRole(Integer id) throws HisException {
        //判断角色id是否符合规范
        if(!StringToTrimUtil.StringToTrim(String.valueOf(id)) | id<1){
            throw new HisException("请确认信息是否正确",500);
        }

        //注销角色
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",id);
        int delete = roleMapper.delete(wrapper);
        if(delete<=0) throw new HisException("注销角色失败",500);

        return true;
    }

    /**
     * 获取所有角色信息
     */
    @Override
    public List<RoleInfo> obtainAllRole() throws HisException {

        List<Role> roles = roleMapper.selectList(null);
        if(null == roles) throw new HisException("获取角色信息失败",500);
        List<RoleInfo> roleInfos = WebBeanUtils.copyList(roles, RoleInfo.class);

        return roleInfos;
    }

    /**
     * 根据角色名查询角色信息
     *
     * @param condition 角色名
     * @return 角色信息
     */
    @Override
    public List<RoleInfo> obtainOneRole(String condition) throws HisException {
        if(!StringToTrimUtil.StringToTrim(condition)){
            throw new HisException("请输入正确的信息",500);
        }

        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like("role_name",condition);
        List<Role> role = roleMapper.selectList(wrapper);
        if(null == role) throw new HisException("角色不存在",500);

        return WebBeanUtils.copyList(role,RoleInfo.class);
    }
}
